package com.newlink.building.common_base.utils.countrycode

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.common_base.databinding.ActivityCountrySelectLayoutBinding
import com.newlink.building.common_base.ext.doNext
import com.newlink.building.common_base.http.Base_RetrofitHelper
import com.newlink.building.common_base.model.bean.CountryCodeBean
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.common_base.utils.SimpleDividerItemDecoration


/**
 * @Author: Jake
 * @Date: 2023-12-28
 * @Description:
 */
class Base_CountryCodeSelectActivityNL : NL_BaseActivity() {

    private var mCountryList: MutableList<CountryCodeBean> = mutableListOf()
    private var mAdapter: Base_CountryCodeAdapter? = null
    private var mTempList: MutableList<CountryCodeBean> = mutableListOf()
    private lateinit var mBinding: ActivityCountrySelectLayoutBinding

    //    var countryCodeList: MutableList<CountryCodeBean> = mutableListOf()
    override fun attachLayoutRes(): View {
        mBinding = ActivityCountrySelectLayoutBinding.inflate(layoutInflater)
        return mBinding.root
    }


    fun refreshCountryCode() {
        Base_RetrofitHelper.service.getAreaCode().doNext {
            if (it.data != null) {
                val countryList = mutableListOf<CountryCodeBean>()
                for (item in it.data) {
                    countryList.add(CountryCodeBean("", item.area, item.code))
                }
                mAllCountryList = countryList
                mCountryList = mAllCountryList
                mAdapter?.setNewInstance(mCountryList)
                mAdapter?.notifyDataSetChanged()
            }
        }
    }

    override fun initData() {
        refreshCountryCode()
        mCountryList = mAllCountryList
        NLog.e("[jake] ..country select .. ${mCountryList.size}")
//        countryCodeList = intent.getSerializableExtra("country_list") as  MutableList<CountryCodeBean>
//        Log.e("[jake] .... ${countryCodeList.size}")
//        if (countryCodeList != null && countryCodeList.size > 0) {
//            mCountryList = countryCodeList
//        }
        val defaultCountry = mDefaultCountryBean
        NLog.e("[jake] ..country default .. ${defaultCountry}")
//        val defaultCountry = intent.getSerializableExtra("default_country") as CountryCodeBean
        defaultCountry?.let {
//            mBinding.recommendCountry.findViewById<TextView>(R.id.country_name).text = it.area
            mBinding.recommendCountry.countryName.text = it.area
            mBinding.recommendCountry.countryCode.text = it.code
        }
        mBinding.recommendCountry.countryLayout.setOnClickListener {
            setResult(1_000_008, intent.putExtra("default_country_code", defaultCountry?.code))
            this@Base_CountryCodeSelectActivityNL.finish()
        }
    }

    override fun initView() {
        mAdapter = Base_CountryCodeAdapter(mCountryList)
        mAdapter!!.setOnItemClickListener { adapter, view, position ->
            if (mCountryList.size > 0) {
                setResult(
                    1_000_008,
                    intent.putExtra("default_country_code", mCountryList[position].code)
                )
                this@Base_CountryCodeSelectActivityNL.finish()
            }
        }
        mBinding.countryList.adapter = mAdapter
        mBinding.countryList.addItemDecoration(SimpleDividerItemDecoration(this, 1))

        mBinding.countrySearchEdit.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable?) {
                if (s == null || s.toString().isEmpty()) {
                    mCountryList = mAllCountryList
                    mAdapter?.setNewInstance(mCountryList)
                    mAdapter?.notifyDataSetChanged()
                } else {
                    val letters = s.toString().lowercase()
                    mTempList.clear()
//                    Log.e("[jake] .... ${mTempList.size}")
//                    Log.e("[jake] .... ${mAllCountryList.size}")
                    for (countryCodeBean in mAllCountryList) {
                        if (countryCodeBean.area.lowercase()
                                .contains(letters) || countryCodeBean.code.lowercase()
                                .contains(letters)
                        ) {
                            mTempList.add(countryCodeBean)
                        }
                    }
                    NLog.e("[jake] .... ${mTempList.size}")
                    mCountryList = mTempList
                    mAdapter?.setNewInstance(mCountryList)
                    mAdapter?.notifyDataSetChanged()
                }
            }


        })

        mBinding.topBarBack.setOnClickListener {
            this@Base_CountryCodeSelectActivityNL.finish()
        }
    }

}